package org.dftproject.genesis.ui.pages.circlediagram;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Path;
import org.eclipse.swt.widgets.Display;

public class GraphicsUtils {

	public static void drawArc(Graphics graphics, Rectangle bounds, float offset, float length) {
		Path path = new Path(Display.getCurrent());
		path.addArc(bounds.x, bounds.y, bounds.width, bounds.height, offset, length);
		
		graphics.drawPath(path);
		
		path.dispose();
	}
	
	public static void fillArc(Graphics graphics, Rectangle bounds, float offset, float length) {
		Path path = new Path(Display.getCurrent());
		path.addArc(bounds.x, bounds.y, bounds.width, bounds.height, offset, length);
	
		path.moveTo(bounds.x + bounds.width / 2 + (float) (bounds.width / 2 * Math.cos(offset * Math.PI / 180.0)), bounds.y + bounds.height / 2 - (float) (bounds.height / 2 * Math.sin(offset * Math.PI / 180.0)));
		path.lineTo(bounds.x + bounds.width / 2 + (float) (bounds.width / 2 * Math.cos((offset + length) * Math.PI / 180.0)), bounds.y + bounds.height / 2 - (float) (bounds.height / 2 * Math.sin((offset + length) * Math.PI / 180.0)));
		path.lineTo(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
		path.close();
		
		graphics.fillPath(path);
		
		path.dispose();
	}
	
	public static void fillArc(Graphics graphics, Rectangle bounds, float offset, float length, int radius) {
		Path path = new Path(Display.getCurrent());
		path.addArc(bounds.x, bounds.y, bounds.width, bounds.height, offset, length);
		
		Rectangle innerBounds = bounds.getCropped(new Insets(radius));
		
		path.moveTo((float) (bounds.x + bounds.width / 2.0 + bounds.width / 2.0 * Math.cos(offset * Math.PI / 180.0)), (float) (bounds.y + bounds.height / 2.0 - bounds.height / 2.0 * Math.sin(offset * Math.PI / 180.0)));
		path.lineTo((float) (bounds.x + bounds.width / 2.0 + bounds.width / 2.0 * Math.cos((offset + length) * Math.PI / 180.0)), (float) (bounds.y + bounds.height / 2.0 - bounds.height / 2.0 * Math.sin((offset + length) * Math.PI / 180.0)));
		path.lineTo((float) (innerBounds.x + innerBounds.width / 2.0 + innerBounds.width / 2.0 * Math.cos((offset + length) * Math.PI / 180.0)), (float) (innerBounds.y + innerBounds.height / 2.0 - innerBounds.height / 2.0 * Math.sin((offset + length) * Math.PI / 180.0)));
		path.lineTo((float) (innerBounds.x + innerBounds.width / 2.0 + innerBounds.width / 2.0 * Math.cos(offset * Math.PI / 180.0)), (float) (innerBounds.y + innerBounds.height / 2.0 - innerBounds.height / 2.0 * Math.sin(offset * Math.PI / 180.0)));
		path.close();
		
		
		path.addArc(innerBounds.x, innerBounds.y, innerBounds.width, innerBounds.height, offset, length);
		
		graphics.fillPath(path);
		
		path.dispose();
	}

}
